package cn.schoolwow.util.domain.check.instance;

import cn.schoolwow.quickflow.QuickFlow;
import cn.schoolwow.quickflow.QuickFlowBuilder;
import cn.schoolwow.util.module.check.instance.flow.*;
import cn.schoolwow.util.module.check.instance.handler.CheckInstanceTryCatchFinallyHandler;

import java.util.List;

public class CheckInstance {
    private static QuickFlow quickFlow = QuickFlowBuilder.newInstance()
            .tryCatchFinallyHandler(new CheckInstanceTryCatchFinallyHandler());

    /**创建实例检查约束*/
    public static CheckInstance newCheck(){
        CheckInstance instanceQuery = new CheckInstance();
        return instanceQuery;
    }

    /**
     * 非空值检查
     * @param value 实际值
     * @param message 提示信息
     * */
    public CheckInstance notNullCheck(Object value, String message){
        quickFlow.startFlow(new CheckInstanceNotNullFlow())
                .putCurrentCompositeFlowData("value", value)
                .putCurrentCompositeFlowData("message", message)
                .execute();
        return this;
    }

    /**
     * 任意一个值不为空即可
     * @param valueArray 实际值列表
     * @param message 提示信息
     * */
    public CheckInstance notNullAnyCheck(Object[] valueArray, String message){
        quickFlow.startFlow(new CheckInstanceNotNullAnyFlow())
                .putCurrentCompositeFlowData("valueArray", valueArray)
                .putCurrentCompositeFlowData("message", message)
                .execute();
        return this;
    }

    /**
     * 非空值检查
     * @param value 实际值
     * @param message 提示信息
     * */
    public CheckInstance notEmptyCheck(Object value, String message){
        quickFlow.startFlow(new CheckInstanceNotEmptyFlow())
                .putCurrentCompositeFlowData("value", value)
                .putCurrentCompositeFlowData("message", message)
                .execute();
        return this;
    }

    /**
     * 布尔值检查
     * @param isTrue 布尔值
     * @param message 提示信息
     * */
    public CheckInstance isTrueCheck(boolean isTrue, String message){
        quickFlow.startFlow(new CheckIsTrueFlow())
                .putCurrentCompositeFlowData("isTrue", isTrue)
                .putCurrentCompositeFlowData("message", message)
                .execute();
        return this;
    }

    /**
     * 字段值检查
     * @param actualValue 实际值
     * @param expectValue 期望值
     * @param message 提示信息
     * */
    public CheckInstance fieldValueCheck(Object actualValue, Object expectValue, String message){
        quickFlow.startFlow(new CheckInstanceFieldValueFlow())
                .putCurrentCompositeFlowData("actualValue", actualValue)
                .putCurrentCompositeFlowData("expectValue", expectValue)
                .putCurrentCompositeFlowData("message", message)
                .execute();
        return this;
    }

    /**
     * 包含值检查
     * @param value 实际值
     * @param allowValueList 允许值列表
     * @param message 提示信息
     * */
    public CheckInstance inValueCheck(Object value, List allowValueList, String message){
        if(null==allowValueList||allowValueList.isEmpty()){
            throw new IllegalArgumentException(message+"允许值列表不能为空");
        }
        quickFlow.startFlow(new CheckInstanceInValueListFlow())
                .putCurrentCompositeFlowData("value", value)
                .putCurrentCompositeFlowData("allowValueList", allowValueList)
                .putCurrentCompositeFlowData("message", message)
                .execute();
        return this;
    }

    /**
     * 列表元素重复值检查
     * @param list 列表
     * @param message 提示信息
     * */
    public CheckInstance notRepeatCheck(List list, String message){
        quickFlow.startFlow(new CheckInstanceNotRepeatFlow())
                .putCurrentCompositeFlowData("list", list)
                .putCurrentCompositeFlowData("message", message)
                .execute();
        return this;
    }

    /**
     * 检查id字段值
     * @param idFieldValue id字段值
     * @param message 提示信息
     * */
    public CheckInstance idFieldCheck(Object idFieldValue, String message){
        quickFlow.startFlow(new CheckInstanceIdFieldFlow())
                .putCurrentCompositeFlowData("idFieldValue", idFieldValue)
                .putCurrentCompositeFlowData("message", message)
                .execute();
        return this;
    }

}
